package com.ysxm.controller.admincontroller;

import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.github.pagehelper.PageInfo;
import com.ysxm.entity.Admin;
import com.ysxm.entity.Admin.admin;
import com.ysxm.service.adminservice.AdminService;
import com.ysxm.util.MailUtils;
import com.ysxm.util.ResponseInfo;

@Controller
public class AdminController {

	@Autowired
	private AdminService adminService;

	// 管理员登录
	@PostMapping(value = "aLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Admin> addCollect(@RequestBody @Validated(value = { Admin.alogin.class }) Admin admin,
			HttpSession session) {

		admin = adminService.adLogin(admin.getAname(), admin.getApassword(), session);
		session.setAttribute("admin", admin);
		return new ResponseInfo<Admin>(ResponseInfo.SUCCESS_CODE, "登录成功", admin);
	}

	// 管理员退出登录
	@GetMapping(value = "aOutLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject aOutLogin(Admin admin, HttpSession session) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "系统繁忙，稍后重试";
		try {
			// 销毁session
			session.invalidate();
			msg = "退出成功";
		} catch (Exception exception) {
			json.put("status", -1);
			json.put("msg", "操作失败，请稍后再试");
			return json;
		}

		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 添加管理员
	@PostMapping(value = "aRegister", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Admin> addAdmin(
			@RequestBody @Validated(value = { Admin.alogin.class, Admin.admin.class }) Admin admin,
			HttpSession session) {
		admin.setApassword(admin.getApassword());
		adminService.addAdmin(admin);
		return new ResponseInfo<Admin>(ResponseInfo.SUCCESS_CODE, "添加成功", admin);
	}

	// 删除管理员
	@GetMapping(value = "delAdmin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<String> delAdmin(int aid) {
		adminService.deleteAdmin(aid);
		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "删除成功");

	}

	// 修改管理员
	@PostMapping(value = "updateAdmin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Admin> updateAdmin(
			@RequestBody @Validated(value = { Admin.alogin.class, Admin.admin.class }) Admin admin,
			HttpSession session) {

		adminService.updateAdmin(admin);
		return new ResponseInfo<Admin>(ResponseInfo.SUCCESS_CODE, "修改成功", admin);
	}

	// 查询单个管理员信息
	@GetMapping(value = "findOneAdmin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Admin> findOneAdmin(Integer aid, HttpSession session) {
		Admin admin = null;
		admin = adminService.findAdminById(aid);
		return new ResponseInfo<Admin>(ResponseInfo.SUCCESS_CODE, "成功进入个人信息", admin);

	}

	// 显示所有管理员信息
	@GetMapping(value = "aFindAll", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<PageInfo<Admin>> findAllAdminPage(
			@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize, HttpSession session,
			PageInfo<Admin> pageInfo) {

		List<Admin> list = adminService.findAllAdmin(pageNum, pageSize);
		pageInfo = new PageInfo<Admin>(list);
		return new ResponseInfo<PageInfo<Admin>>(ResponseInfo.SUCCESS_CODE, "显示所有管理员信息", pageInfo);

	}

	// 发送邮箱
	@RequestMapping(value = "sendEmailCode", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<Void> sendEmailCode(String email, HttpSession session)
			throws AddressException, MessagingException {
		// 生成随机验证码
		String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
		// 将验证码存入session
		session.setAttribute("code", code);
		// 时效
		/* session.setMaxInactiveInterval(5*60); */
		// 将验证码作为邮件内容参数
		MailUtils.sendMail(email, "您的修改密码验证码是" + code);

		return new ResponseInfo<Void>(ResponseInfo.SUCCESS_CODE, "已发送邮件，请查收");
	}

	// 找回密码
	@RequestMapping(value = "updataNewPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateNewPassword(@RequestBody JSONObject json, HttpSession session) {
		String email = json.getString("email");
		String password = json.getString("apassword");
		String secondPassword = json.getString("secondPassword");
		String code = json.getString("code");
		int status = 0;
		String msg = "操作成功";
		String code2 = (String) session.getAttribute("code");
		if (session.getAttribute("code") != null) {
			if (code.equals(code2)) {
				if (password.equals(secondPassword)) {
					adminService.findAdminByUE(secondPassword, email);
					json.put("status", status);
					json.put("msg", msg);
					return json;
				} else {
					json.put("status", -1);
					json.put("msg", "输入密码不一致");
					return json;
				}
			} else {
				json.put("status", -1);
				json.put("msg", "验证码不正确");
				return json;
			}
		} else {
			json.put("status", -1);
			json.put("msg", "验证码已过期");
			return json;
		}
	}
}
